//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class TrendlineLabel : IOfficeXmlSerializable, IShape, INotifyPropertyChanged
    {
        private ExtensionElement _extension;


        private Layout layout;
        private ChartFormat numFmt;
        private ShapeProperties spPr;

        private Text tx;
        private TextBody txPr;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension");}
        }

        public Layout Layout
        {
            get { return layout; }
            set { layout = value; notifyPropertyChanged("Layout"); }
        }

        public Text Text
        {
            get { return tx; }
            set { tx = value; notifyPropertyChanged("Text"); }
        }

        public ChartFormat Format
        {
            get { return numFmt; }
            set { numFmt = value; notifyPropertyChanged("Format"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return spPr; }
            set { spPr = value; notifyPropertyChanged("ShapeProperties"); }
        }

        public TextBody TextProperties
        {
            get { return txPr; }
            set { txPr = value; notifyPropertyChanged("TextProperties"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("trendlineLbl"))
            {
                Layout = c.ReadOptionalElement<Layout>("layout");
                Text = c.ReadOptionalElement<Text>("tx");
                Format = c.ReadOptionalElement<ChartFormat>("numFmt");
                ShapeProperties = c.ReadOptionalElement<ShapeProperties>("spPr");
                TextProperties = c.ReadOptionalElementAs<TextBody>("txPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("trendlineLbl"))
            {
                c.WriteOptionalElement(Layout);
                c.WriteOptionalElement(Text);
                c.WriteOptionalElement(Format);
                c.WriteOptionalElement(ShapeProperties);
                c.WriteOptionalElementAs(TextProperties, "txPr");
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}